<?php

    // Top 5 Most Categories

    if($faq != '')
      {
        $popcat_faqsql = "AND f_id = $faq";
      }

    $query_popcat = "SELECT c_id, COUNT(*) as hits FROM orbitfaq_stats WHERE c_id != '0' $popcat_faqsql GROUP BY c_id ORDER BY `hits` DESC LIMIT 5";
    $result_popcat = $faqsql_query ($query_popcat)OR DIE( "$sql_query_error $query_popcat");

    while ($row_popcat = $faqsql_fetch_array ($result_popcat)){
      $popcat_cid = $row_popcat[0];
      $popcat_count = $row_popcat[1];

      $query_popcat_title = "SELECT title FROM orbitfaq_categories WHERE id = '$popcat_cid' LIMIT 1";
      $result_popcat_title = $faqsql_query ($query_popcat_title)OR DIE( "$sql_query_error $query_popcat_title");

      while ($row_popcat_title = $faqsql_fetch_array ($result_popcat_title)){
        $popcat_title = $row_popcat_title[0];
      }

      $popcat_title = html_entity_decode($popcat_title);

       $smarty_top5cat_array[] = array(
          "popcat_title" => $popcat_title,
          "popcat_id" => $popcat_id,
          "popcat_count" => $popcat_count
        );
    }

    // Top 5 Recent Questions
    if($faq != '')
      {
        $recent_faqsql = "AND f_id = $faq";
      }

    $query_recent = "SELECT * FROM `orbitfaq_questions` WHERE a_id != '' AND `status` = '1' $recent_faqsql ORDER BY `posted_time` DESC LIMIT 5";
    $result_recent = $faqsql_query ($query_recent)OR DIE( "$sql_query_error $query_recent");

    while ($row_recent = $faqsql_fetch_array ($result_recent)){
      $recent_qid = $row_recent[0];
      $recent_fid = $row_recent[1];
      $recent_cid = $row_recent[2];
      $recent_qtitle = $row_recent[4];

      $recent_qtitle = html_entity_decode($recent_qtitle);
      $recent_qtitle = strip_tags($recent_qtitle);

       $smarty_top5recent_array[] = array(
          "recent_qid" => $recent_qid,
          "recent_fid" => $recent_fid,
          "recent_cid" => $recent_cid,
          "recent_qtitle" => $recent_qtitle
        );
    }

     $rated_count = 1;

    // Top 5 Most Rating Questions

    if($faq != '')
      {
        $rate_faqsql = "AND f_id = $faq";
      }

    $query_rate = "SELECT q_id, AVG(rating) as rated FROM orbitfaq_ratings GROUP BY orbitfaq_ratings.q_id ORDER BY `rated` DESC";
    $result_rate = $faqsql_query ($query_rate)OR DIE( "$sql_query_error $query_rate");

    while ($row_rate = $faqsql_fetch_array ($result_rate)){
      $rate_qid = $row_rate[0];
      $rate_count = $row_rate[1];

      $query_rate_qn = "SELECT * FROM `orbitfaq_questions` WHERE id = '$rate_qid' AND `status` = '1' $rate_faqsql ORDER BY `posted_time` DESC LIMIT 1";
      $result_rate_qn = $faqsql_query ($query_rate_qn)OR DIE( "$sql_query_error $query_rate_qn");

      while ($row_rate_qn = $faqsql_fetch_array ($result_rate_qn)){
        $rate_fid = $row_rate_qn[1];
        $rate_cid = $row_rate_qn[2];
        $rate_qtitle = $row_rate_qn[4];

        $rate_qtitle = html_entity_decode($rate_qtitle);
        $rate_qtitle = strip_tags($rate_qtitle);

        if($rated_count <= 5)
          {

           $smarty_top5rate_array[] = array(
              "rate_qid" => $rate_qid,
              "rate_fid" => $rate_fid,
              "rate_cid" => $rate_cid,
              "rate_qtitle" => $rate_qtitle,
              "rate_count" => $rate_count
            );

            $rated_count++;

          }

      }

    }

    // Top 5 Most Active Users

    if($faq != '')
      {
        $rate_usersql = "AND f_id = $faq";
      }

    $query_users = "SELECT * FROM orbitfaq_users WHERE status = '1'";
    $result_users = $faqsql_query ($query_users)OR DIE( "$sql_query_error $query_users");

    while ($row_users = $faqsql_fetch_array ($result_users)){
      $users_uid = $row_users[0];
      $users_fid = $row_users[1];
      $users_login = $row_users[2];
      $users_fullname = $row_users[3];
      $users_fullname = html_entity_decode($users_fullname);

      $users_score = "0";

      $query_users_qn = "SELECT COUNT(*) FROM orbitfaq_questions WHERE posted_email = '$users_login' $rate_usersql GROUP BY posted_email;";
      $result_users_qn = $faqsql_query ($query_users_qn)OR DIE( "$sql_query_error $query_users_qn");

      while ($row_users_qn = $faqsql_fetch_array ($result_users_qn)){
        $users_qn_count = $row_users_qn[0];
        $this_qn_score = $users_qn_count*10;
        $users_score = $users_score+$this_qn_score;
      }

      $query_users_rating = "SELECT COUNT(*) FROM orbitfaq_ratings WHERE u_id = '$users_uid' GROUP BY u_id;";
      $result_users_rating = $faqsql_query ($query_users_rating)OR DIE( "$sql_query_error $query_users_rating");

      while ($row_users_rating = $faqsql_fetch_array ($result_users_rating)){
        $users_rating_count = $row_users_rating[0];
        $this_rating_score = $users_rating_count*5;
        $users_score = $users_score+$this_rating_score;
      }

      $users_score_array[] = array(
          "users_uid" => "$users_uid",
          "users_login" => "$users_login",
          "users_fullname" => "$users_fullname",
          "users_score" => "$users_score"
        );


    }

    $smarty_top5users_array = columnSort($users_score_array, "users_score");



  // Categories
  $smarty->assign('Top5CatListingTitle','Categories');
  $smarty->assign('Top5CatListing',$smarty_top5cat_array);

  // Recent Question
  $smarty->assign('Top5RecentListingTitle','Most Recent Questions');
  $smarty->assign('Top5RecentListing',$smarty_top5recent_array);

  // Question Rating
  $smarty->assign('Top5RatingListingTitle','Highest Rating Questions');
  $smarty->assign('Top5RatingListing',$smarty_top5rate_array);

  // Active User
  $smarty->assign('Top5UserListingTitle','Most Active Users');
  $smarty->assign('Top5UserListing',$smarty_top5users_array);

?>